/**
 * @file com.demon.esport.register.eureka.server.config
 * @CopyRight (C) www.10-game.com
 * @author jeffreychan
 * @email mebeyfeng@qq.com
 * @date 2018/11/30 23:58
 */

package com.demon.esport.register.eureka.server.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;

/**
 * @author: jeffreychan
 * @Date: 2018/11/30 23:58
 * @Description: 服务注册配置
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable();
        //注意：为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic,如果是form方式,不能使用url格式登录
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new DefaultPasswordEncoder())
                //admin
                .withUser("admin").password("admin").roles("EUREKA-CLIENT").and()
                //eureka-security-client
                .withUser("client").password("10@games").roles("EUREKA-CLIENT");
    }
}
